package screen_calibration;

import java.awt.Point;

/**
 * This class produces 3-color proportional mixer.
 * @author Alex Frid alex.frid@gmail.com; Ran Dror
 */
class Mixer extends TripleSlider {
    
    /** Parent applet reference */
    CanvasLabel imgTest;
    
    /** Current resulting color point */
    Point current;
    
    /**
     * Constructs the 3-color mixer.
     * @param imgTest parent applet reference.
     */
    public Mixer( CanvasLabel imgTest ){
        
        super();
        
        this.imgTest = imgTest;
        
        current = new Point(0,0);
        
        // set colors triple-slider
        setColor( 0, imgTest.chroma.pointColor( imgTest.chroma.gamutPoint[0] ) );
        setColor( 1, imgTest.chroma.pointColor( imgTest.chroma.gamutPoint[1] ) );
        setColor( 2, imgTest.chroma.pointColor( imgTest.chroma.gamutPoint[2] ) );
        
        // values are set to 1/3
        setEnabled( true );
        
        updateCurrentPoint();
        setRGB();
        
    }
    
    /** Processes slider changes. */
    public void Motion() {
        
        updateCurrentPoint();
        
        // update rgbLocal
        setRGB();
        
        imgTest.compColor.update();
        
        // move current-position marker on the diagram
        imgTest.chroma.repaint(); // this will call update(g)
        
        
    }
    
    /**
     * Returns current point.
     * @return current point coordinates.
     */
    public Point getCurrentPoint() {
        return current;
    }
    
    private void updateCurrentPoint() {
        double a, b, c;
        
        a = getValue(0);
        b = getValue(1);
        c = getValue(2);
        
        current.x = (int)(
                a * imgTest.chroma.gamutPoint[0].x +
                b * imgTest.chroma.gamutPoint[1].x +
                c * imgTest.chroma.gamutPoint[2].x );
        
        current.y = (int)(
                a * imgTest.chroma.gamutPoint[0].y +
                b * imgTest.chroma.gamutPoint[1].y +
                c * imgTest.chroma.gamutPoint[2].y );
    }
    
    
    private void setRGB() {
        int value = imgTest.chroma.arr[current.x][current.y];
        
        int r = (value & 0x00ff0000) >> 16;
        int g = (value & 0x0000ff00) >> 8;
        int b = (value & 0x000000ff);
        
        imgTest.rgbLocal.setRGB( r, g, b );
    }
    
    
}

